﻿namespace FitnessTrackerUI.Command
{
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Globalization;
    using System.Windows.Input;
    using FitnessTracker.Entities;
    using FitnessTracker.Observables;
    using FitnessTracker.Observers;
    using FitnessTrackerUI.Commands;

    class WorkoutCommands
    {
        private DelegateCommand startWorkoutCommand;
        private DelegateCommand stopWorkoutCommand;
        private readonly FitnessTrackerEntities fitnessTrackerEntities = new FitnessTrackerEntities();
        private IDisposable dataLoggingCancellation;
        private DataLogger dataLogger;
        private Workout workout;
        private bool isStartWorkoutCommandApplicable = true;
        private bool isStopWorkoutCommandApplicalbe;

        public ICommand StartWorkoutCommand
        {
            get
            {
                return this.startWorkoutCommand
                       ?? (this.startWorkoutCommand = new DelegateCommand(this.StartWorkout, this.CanStartWorkout));
            }
        }

        public ICommand StoptWorkoutCommand
        {
            get
            {
                return this.stopWorkoutCommand
                       ?? (this.stopWorkoutCommand = new DelegateCommand(this.StopWorkout, this.CanStopWorkout));
            }
        }

        private void StartWorkout()
        {
            this.SetupWorkout();
            this.StartDataLooging();
            this.isStartWorkoutCommandApplicable = false;
            this.isStopWorkoutCommandApplicalbe = true;
        }

        private bool CanStartWorkout()
        {
            return isStartWorkoutCommandApplicable;
        }

        private void SetupWorkout()
        {
            if (!this.fitnessTrackerEntities.Users.Any(u => u.userID.Equals("alextc")))
            {
                throw new Exception("No user information was found in the database. Please, create your profile before starting workout");
            }

            var exersiser = this.fitnessTrackerEntities.Users.Single(u => u.userID.Equals("alextc"));

            this.workout = new Workout
            {
                User = exersiser,
                workoutDate = DateTime.Now,
                workoutName = exersiser.userID + "-" + DateTime.Now.ToString(CultureInfo.InvariantCulture)
            };

            this.fitnessTrackerEntities.Workouts.Add(this.workout);
            this.fitnessTrackerEntities.SaveChanges();
        }

        private void StartDataLooging()
        {
            Debug.WriteLine("Entering StartDataLoggin Method");
            dataLogger = new DataLogger(workout.workoutID);
            var hxm = HxMReader.GetInstance();
            Debug.Assert(hxm != null);
            dataLoggingCancellation = hxm.Subscribe(dataLogger);
        }

        private void StopWorkout()
        {
            this.dataLoggingCancellation.Dispose();
            this.isStartWorkoutCommandApplicable = true;
            this.isStopWorkoutCommandApplicalbe = false;
        }

        private bool CanStopWorkout()
        {
            return isStopWorkoutCommandApplicalbe;
        }
    }
}
